package org.jash.launcher.ui

import android.app.ProgressDialog
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.lifecycle.lifecycleScope
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.google.android.material.dialog.MaterialDialogs
import kotlinx.coroutines.launch
import org.jash.common.adapter.CommonAdapter
import org.jash.common.mvi.BaseFragment
import org.jash.launcher.R
import org.jash.launcher.BR
import org.jash.launcher.databinding.FragmentMessageBinding
import org.jash.launcher.entity.MessageEntity
import org.jash.launcher.model.MessageIntent
import org.jash.launcher.model.MessageState
import org.jash.launcher.model.MessageViewModel

/**
 * A simple [Fragment] subclass.
 * Use the [MessageFragment.newInstance] factory method to
 * create an instance of this fragment.
 */
class MessageFragment : BaseFragment<FragmentMessageBinding, MessageViewModel>() {
    private val adapter by lazy { CommonAdapter<MessageEntity>({R.layout.item_message to BR.message}) }
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        binding.recycler.adapter = adapter
        lifecycleScope.launch {
            viewModel.intent.send(MessageIntent.GetMessage(1))
        }
    }
    fun loadedMessage(res:MessageState.Response) {
        adapter += res.data
    }

    companion object {
        /**
         * Use this factory method to create a new instance of
         * this fragment using the provided parameters.
         * @return A new instance of fragment MessageFragment.
         */
        // TODO: Rename and change types and number of parameters
        @JvmStatic
        fun newInstance() =
            MessageFragment()
    }
}